
import JJUtil from "JJUtil.js";

class JJPageUtil {

    // 获取当前页面的对象
    static getCurrentPage() {
        const pages = getCurrentPages();
        const currPage = pages[pages.length - 1];
        return currPage;
    }

    // 获取上一个页面的对象
    static getSupperPage() {
        const pages = getCurrentPages();
        const currPage = pages[pages.length - 2];
        return currPage;
    }

    // 返回首页
    static popToRootPage() {
        let pages = getCurrentPages();
        wx.navigateBack({
            delta: pages.length - 1
        })
    }

    // 获取当前页的index
    static getCurrentPagePopIndex(route) {
        let pages = getCurrentPages();
        // 默认返回-1,找不到当前页面
        let mePopIndex = -1;
        for (let i = pages.length - 1; i >= 0; i--) {
            let page = pages[i];
            if (JJUtil.isNotNull(page.route) && JJUtil.isNotNull(route) && page.route === route) {
                mePopIndex = pages.length - 1 - i;
            }
        }
        return mePopIndex;
    }
}

export default JJPageUtil;
